package nu.mine.tbje.easyEDT;

import java.io.BufferedOutputStream;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import nu.mine.tbje.easyEDT.data.Cours;

public class vcalWriter {
	public static String getWeek(int week, int id, OutputStream out) {
		OutputStreamWriter o;
		try {
			o = new OutputStreamWriter(new BufferedOutputStream(out), "UTF-8");
			Edt edt = new Edt();
			edt.setLogin("adeweb");
			edt.setPass("ade423");
			edt.doLogin();
			edt.doInit();
			edt.setIdAndCat("trainee", id);
			edt.setDisplayOptions();
			if (week!=-1){
				edt.setWeek(week);
			}
			Calendar cal = GregorianCalendar.getInstance();
			InputStream i = edt.getData();
			Iterator<Cours> iterator = edt.parse(i).iterator();
			Cours cours = null;
			o.write("BEGIN:VCALENDAR\n" +
					"VERSION:1.0\n" +
					"X-WR-CALDESC:Emploi du temps INSA generé par tbje.mine.nu/easyEDT\n" +
					"X-LIC-LOCATION:Europe/Paris\n" +
					"X-WR-CALNAME:easyEDT\n" +
					"PRODID:-//tbje.mine.nu/easyEDT\n" +
					"CALSCALE:GREGORIAN\n" +
					"METHOD:PUBLISH\n");
			Pattern p1 = Pattern.compile("([0-9]{1,2})h([0-9]{2})min");
			Pattern p2 = Pattern.compile("([0-9]{1,2})h");
			Pattern p3 = Pattern.compile("([0-9]{1,2})min");
			Matcher m;
			while (iterator.hasNext()){
				cours = iterator.next();
				SimpleDateFormat d1 = new SimpleDateFormat("dd/MM/yyyy-HH'h'mm");
				SimpleDateFormat d2 = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
				cal.setTime(d1.parse(cours.getDate()+"-"+cours.getTime()));
				//cal.add(Calendar.HOUR, Integer.parseInt(m.group(1)));
				o.write("BEGIN:VEVENT\n");
				o.write("DTSTART:" +d2.format(cal.getTime()) +"\n");
				
				// Calculate duration using regexp and calendar
				m = p1.matcher(cours.getDuration());
				if(m.find()){
					cal.add(Calendar.HOUR_OF_DAY, Integer.parseInt(m.group(1)));
					cal.add(Calendar.MINUTE, Integer.parseInt(m.group(2)));
				} else {
					m = p2.matcher(cours.getDuration());
					if (m.find()) {
						cal.add(Calendar.HOUR_OF_DAY, Integer.parseInt(m.group(1)));
					} else {
						m = p3.matcher(cours.getDuration());
						m.find();
						cal.add(Calendar.MINUTE, Integer.parseInt(m.group(1)));
					}
				}
				o.write("DTEND:" +d2.format(cal.getTime()) +"\n");
				o.write("SUMMARY;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:"+ cours.getName()+ " (" + cours.getTeacher() + ")\n");
				o.write("LOCATION;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:"+cours.getRoom()+"\n");
				o.write("DESCRIPTION;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE: http://tbje.mine.nu/easyEDT/\n");
				o.write("CATEGORIES:INSA\n");
				o.write("END:VEVENT\n");
			}
			o.write("END:VCALENDAR\n");
			o.flush();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "ok";
	}
}
